// component/home/com-index-house-item/com-index-house-item.js
import api from "../../../common/api";

const defaultImg = '/images/defaultImgSmall.jpg'
const _constant = {
  TAB_MANAGE: 'manage',
  TAB_INFO: 'info',
  TAB_DEVELOPMENT: 'development',
}

Component({

  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/,
  },

  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    defaultImg: '',
    isShowPopouer: false,
    popouer: [],
  },

  /**
   * 组件的数据监听
   */
  observers: {
    item(val) {
      if (val.state === 'AVAILABLE') {
        this.setData({
          popouer: [{
              label: '下架房源',
              value: 'OUT'
            },
            {
              label: '删除房源',
              value: 'DEL'
            },
            {
              label: '修改房源',
              value: 'EDIT'
            }
          ]
        })
      }
      if (val.state === 'UNAVAILABLE') {
        this.setData({
          popouer: [{
              label: '上架房源',
              value: 'PUT'
            },
            {
              label: '删除房源',
              value: 'DEL'
            },
            {
              label: '修改房源',
              value: 'EDIT'
            }
          ]
        })
      }

    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleShare() {
      const {
        hou_del_id
      } = this.data.item || {}
      this.triggerEvent("onhandleshare", hou_del_id)
    },

    handleImgError() {
      this.setData({
        defaultImg: defaultImg
      })
    },

    onJumpDetail() {
      this.triggerEvent('jump', {
        tab: _constant.TAB_MANAGE
      })
    },

    onJumpDynamicDetail() {
      this.triggerEvent('jump', {
        tab: _constant.TAB_DEVELOPMENT
      })
    },

    // handleGoToScanWeb() {
    //   this.triggerEvent("handleGoToScanWeb")
    // },

    // 跳转修改房源
    jumpEditHouse() {
      const {
        hou_del_id
      } = this.data.item || {}
      wx.navigateTo({
        url: `/pages/addHouse/infoEntryHouse/infoEntryHouse?houDelId=${hou_del_id}&flag=0`,
      });
    },

    delHouse() {
      const {
        hou_del_id
      } = this.data.item || {}
      wx.showModal({
        title: '提示',
        content: '是否确认删除房源?',
        success: (res) => {
          if (res.confirm) {
            api.deleteHouseByIds({
              ids: [hou_del_id],
            }, (res) => {
              if (res && res.data && res.data.isSuccess && res.data.content && res.data.content.successCount >= 1) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'none',
                })
                this.triggerEvent('refresh')
              } else {
                if (res && res.data && res.data.content && res.data.content.errorDescription && res.data.content.errorDescription.length) {
                  this.delTips(res.data.content.errorDescription[0].code)
                } else {
                  wx.showToast({
                    title: '删除失败，请稍后再试~',
                    icon: 'none',
                  })
                }
              }
            }, () => {
              wx.showToast({
                title: '删除失败，请稍后再试~',
                icon: 'none',
              })
            });
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },

    onPopoverSelect(e) {
      const value = e.detail
      switch (value) {
        case 'PUT':
          this.putHouse()
          break;
        case 'OUT':
          this.outHouse()
          break;
        case 'DEL':
          this.delHouse()
          break;
        case 'EDIT':
          this.jumpEditHouse()
          break;
      }
    },

    onPopoverClick(e) {
      const target = e.detail
      const parent = this.selectOwnerComponent()
      let components = parent.selectAllComponents(".house-item").map(item => {
        return item.selectComponent("#popover")
      })
      components.forEach(item => {
        if (item !== target) {
          item.onHide()
        }
      })
    },

    onScroll() {
      this.selectComponent("#popover") && this.selectComponent("#popover").onHide()
    },

    onClick() {
      // this.selectComponent("#popover") && this.selectComponent("#popover").onHide()
    },

    outHouse() {
      const {
        hou_del_id,
        delegation_type,
        house_id,
        state
      } = this.data.item || {}
      wx.navigateTo({
        url: `/pages/house/houseUndercarriage/houseUndercarriage?delId=${hou_del_id}&state=${state}&style=${delegation_type}&houseId=${house_id}&invalidType=1`
      });

      // api.checkDelegationType({
      //     houseId: houseId,
      //     delegationType: style === 'SALES' ? 'RENT' : 'SALES',
      //   },
      //   (res) => {
      //     if (res.data.content.length == 0) {
      //       wx.navigateTo({
      //         url: "/pages/house/houseUndercarriage/houseUndercarriage?delId=" +
      //           delId +
      //           "&state=" +
      //           state +
      //           "&style=" +
      //           style +
      //           "&houseId=" +
      //           houseId +
      //           "&invalidType=1",
      //       });
      //     }
      //      else {
      //       wx.showModal({
      //         content: `该房源同时存在一套在${style == "SALES" ? "租" : "售"}房源，是否一起下架？`,
      //         cancelColor: "#ccc",
      //         confirmColor: "#f64e4e",
      //         cancelText: "否",
      //         confirmText: "是",
      //         success(res) {
      //           if (res.confirm) {
      //             wx.navigateTo({
      //               url: "/pages/house/houseUndercarriage/houseUndercarriage?delId=" +
      //                 delId +
      //                 "&state=" +
      //                 state +
      //                 "&style=both" +
      //                 "&invalidType=0" +
      //                 "&houseId=" +
      //                 houseId,
      //             });
      //           } else if (res.cancel) {
      //             wx.navigateTo({
      //               url: "/pages/house/houseUndercarriage/houseUndercarriage?delId=" +
      //                 delId +
      //                 "&state=" +
      //                 state +
      //                 "&style=" +
      //                 style +
      //                 "&houseId=" +
      //                 houseId +
      //                 "&invalidType=1",
      //             });
      //           }
      //         },
      //       });
      //     }
      //   }
      // );
    },

    putHouse() {
      const {
        hou_del_id,
        house_id,
      } = this.data.item || {}
      wx.showModal({
        title: '提示',
        content: '是否重新上架房源?',
        success: (res) => {
          if (res.confirm) {
            api.houseFail("", {
              delId: hou_del_id,
              state: "AVAILABLE",
              houseId: house_id,
              invalidType: 1,
            }, (result) => {
              if (result && result.data && result.data.isSuccess) {
                this.triggerEvent('refresh')
              } else {
                wx.showToast({
                  icon: 'none',
                  title: '上架失败，请稍后再试',
                })
              }
            });
          }
        }
      })

    },

    delTips(code) {
      const options = {
        21001: '该房源图片下载中，无法删除该房源。',
        21002: '该房源合成视频中，无法删除该房源。',
        21003: '该房源vr下载中，无法删除该房源。',
        21004: '该房源全景图合成中，无法删除该房源。',
        21005: '该房源发房中，无法删除该房源。',
        21006: '该房源已发布合作房源不能删除。',
      }
      wx.showToast({
        icon: 'none',
        title: options[code] || '删除失败，请稍后再试~',
      })
    },

  }
})